package com.dkd.flow.controller;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dkd.flow.domain.EProcessDefinition;
import com.dkd.flow.domain.EProcessInstances;
import com.dkd.flow.service.IEProcessDefinitionService;
import com.dkd.flow.service.IEProcessInstancesService;
import com.dkd.flow.utils.NodeUtils;
import com.dkd.flow.utils.RevokeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.dkd.common.annotation.Log;
import com.dkd.common.core.controller.BaseController;
import com.dkd.common.core.domain.AjaxResult;
import com.dkd.common.enums.BusinessType;
import com.dkd.flow.domain.EProcessTask;
import com.dkd.flow.service.IEProcessTaskService;
import com.dkd.common.utils.poi.ExcelUtil;
import com.dkd.common.core.page.TableDataInfo;
/**
 * 流程任务Controller
 *
 * @author itheima
 * @date 2025-02-27
 */
@Api(tags = "流程任务Controller")
@RestController
@RequestMapping("/flow/task")
public class EProcessTaskController extends BaseController {
    @Autowired
    RevokeUtils revokeUtils;
    @Autowired
    private NodeUtils nodeUtils;
    @Autowired
    private IEProcessTaskService ieProcessTaskService;
    @Autowired
    IEProcessInstancesService ieProcessInstancesService;
    @Autowired
    IEProcessDefinitionService ieProcessDefinitionService;
    /**
     * 查询流程任务列表
     */
    @ApiOperation("查询流程任务列表")
    @PreAuthorize("@ss.hasPermi('flow:task:list')")
    @GetMapping("/list")
    public TableDataInfo list(EProcessTask eProcessTask) {
        startPage();
        List<EProcessTask> list = ieProcessTaskService.selectEProcessTaskList(eProcessTask);
        return getDataTable(list);
    }
    /**
     * 导出流程任务列表
     */
    @ApiOperation("导出流程任务列表")
    @PreAuthorize("@ss.hasPermi('flow:task:export')")
    @Log(title = "流程任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,
                       EProcessTask eProcessTask) {
        List<EProcessTask> list = ieProcessTaskService.selectEProcessTaskList(eProcessTask);
        ExcelUtil<EProcessTask> util = new ExcelUtil<EProcessTask>(EProcessTask.class);
        util.exportExcel(response,
                list,
                "流程任务数据");
    }
    /**
     * 获取流程任务详细信息
     */
    @ApiOperation("获取流程任务详细信息")
    @PreAuthorize("@ss.hasPermi('flow:task:query')")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID) {
        return success(ieProcessTaskService.selectEProcessTaskByID(ID));
    }
    /**
     * 新增流程任务
     */
    @ApiOperation("新增流程任务")
    @PreAuthorize("@ss.hasPermi('flow:task:add')")
    @Log(title = "流程任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EProcessTask eProcessTask) {
        return toAjax(ieProcessTaskService.insertEProcessTask(eProcessTask));
    }
    /**
     * 修改流程任务
     */
    @ApiOperation("修改流程任务")
    @PreAuthorize("@ss.hasPermi('flow:task:edit')")
    @Log(title = "流程任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EProcessTask eProcessTask) {
        return toAjax(ieProcessTaskService.updateEProcessTask(eProcessTask));
    }
    /**
     * 删除流程任务
     */
    @ApiOperation("删除改流程任务")
    @PreAuthorize("@ss.hasPermi('flow:task:remove')")
    @Log(title = "流程任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable Long[] IDs) {
        return toAjax(ieProcessTaskService.deleteEProcessTaskByIDs(IDs));
    }
    /**
     * 查询本人的待办
     */
    @ApiOperation("查询本人的待办")
    @GetMapping(value = "/todoList")
    public TableDataInfo getTodoList(@RequestParam(required = false) String createTime) {
        Long userId = getUserId();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("approver",
                userId);
        //这里少了时分秒，要用like
        if (createTime != null) {
            queryWrapper.like("create_time",
                    createTime);
        }
        startPage();
        List<EProcessTask> list = ieProcessTaskService.list(queryWrapper);
        List<EProcessTask> resultList = new ArrayList<>();
        if (list.size() > 0) {
            resultList = list.stream()
                    .map(item -> {
                        item.setApproverName(getUsername());
                        return item;
                    })
                    .collect(Collectors.toList());
        }
        return getDataTable(resultList);
    }
    /**
     * 获取流程任务详细信息
     */
    @ApiOperation("获取流程任务详细信息")
    @PostMapping(value = "/getTaskDetail")
    public AjaxResult getTaskDetail(@RequestBody JSONObject data) {
        EProcessTask eProcessTask = ieProcessTaskService.getTaskDetail(data);
        return success(eProcessTask);
    }
}
